package com.hs.pre.module.perinfo;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.hs.HsBase;
import com.hs.core.action.BaseAction;
import com.hs.core.db.DBOperator;
import com.hs.core.db.DatabaseConnection;
import com.hs.core.db.HsPreparedStatement;
import com.hs.log.HsLogger;
import com.hs.pre.model.JobEntry;
import com.hs.pre.model.PreEntry;

 

public class PreManageAction extends BaseAction{
private static final HsLogger logger = HsLogger.getLogger(PreManageAction.class.getName());
public ActionForward execute(ActionMapping mapping, ActionForm form,
		HttpServletRequest request, HttpServletResponse response) throws Exception {
	String action = request.getParameter("action");
	String forward = null;
	if("edit".equals(action)){
		PreEntry entry = null;
		String preId = request.getParameter("preId");  
		try { 
				DBOperator dbo = DatabaseConnection.getDBOperator(); 
				if(null != preId && !"".equals(preId)){
					List data = dbo.select(HsBase.getAnsiSqlMap().get("preinfo.select_one")+"",new Object[]{new Integer(preId)});
					if(data.size()>0)
						entry = new PreEntry((Map)data.get(0));
				}
			} catch (Exception e) { 
				e.printStackTrace();
			}
		request.setAttribute("entry", entry);
		forward = "edit";
	}
	if("save".equals(action)){
		PreEntry entry = null;
		String preId = request.getParameter("preId");
		try {   
			    
				DBOperator dbo = DatabaseConnection.getDBOperator();
				int count = 0;
				String fangm = request.getParameter("fangm");//方名
				String biem = request.getParameter("biem");//别名
				String chufly = request.getParameter("chufly");//处方来源
				String yaowzc = request.getParameter("yaowzc");//药物组成
				String jiaj = request.getParameter("jiaj");//加减
				String gongx = request.getParameter("gongx");//功效
			    String zhuz = request.getParameter("zhuz");//主治
			    String id = request.getParameter("id");//编号
			    String zhibff = request.getParameter("zhibff");//制备方法
			    String yongfyl = request.getParameter("yongfyl");//用法用量
			    String yongyjj = request.getParameter("yongyjj");//用药禁忌
			    String lincyy = request.getParameter("lincyy");//临床应用
			    String yaolzy = request.getParameter("yaolzy");//药理作用
			    String gejls = request.getParameter("gejls");//各家论述
			    String beiz = request.getParameter("beiz");//备注； 
			    String suoyinzt = PreEntry.INDEX_STATUS_ONE;//索引状态
			    String suoyinsj = "";//索引时间 
			    String indexType = request.getParameter("indexType");
			    
			    Date addDate = new Date(System.currentTimeMillis());
				if(StringUtils.isBlank(preId)){
//					count = dbo.update(HsBase.getAnsiSqlMap().get("preinfo.insert")+"",new Object[]{
//						fangm,biem,chufly,yaowzc,jiaj,gongx,zhuz,id,zhibff,yongfyl,yongyjj,lincyy,yaolzy,gejls,beiz,suoyinzt,suoyinsj,addDate
//					});
					HsPreparedStatement hsp = new HsPreparedStatement(HsBase.getAnsiSqlMap().get("preinfo.insert")+"");
					hsp.setString(1, fangm);
					hsp.setString(2, biem);
					hsp.setString(3, chufly);
					hsp.setString(4, yaowzc);
					hsp.setString(5, jiaj);
					hsp.setString(6, gongx);
					hsp.setString(7, zhuz);
					hsp.setString(8, id);
					hsp.setString(9, zhibff);
					hsp.setString(10, yongfyl);
					hsp.setString(11, yongyjj);
					hsp.setString(12, lincyy);
					hsp.setString(13, yaolzy);
					hsp.setString(14, gejls);
					hsp.setString(15, beiz);
					hsp.setString(16, suoyinzt);
					hsp.setString(17, suoyinsj);
					hsp.setTimestamp(18, new Timestamp(System.currentTimeMillis()));
					hsp.setString(19, PreEntry.getFlagByVersion());
					preId = hsp.executeUpdateReturnId();
					//添加任务
					JobEntry job = new JobEntry();
					job.setExeClass("com.hs.job.task.ExeIndexTask");
					job.setJobDesc("方剂数据索引");
					job.setJobType(indexType);
					job.setRelationId(id);
					job.insertJob();
				}else{
					count = dbo.update(HsBase.getAnsiSqlMap().get("preinfo.update_all")+"",new Object[]{
						fangm,biem,chufly,yaowzc,jiaj,gongx,zhuz,id,zhibff,yongfyl,yongyjj,lincyy,yaolzy,gejls,beiz,addDate,preId
					});
				} 
			} catch (Exception e) { 
				e.printStackTrace();
			}
		request.setAttribute("entry", entry);
		request.setAttribute("refresh", "true");
		return new ActionForward("/manage.do?action=edit&preId="+preId);
	}
    if("delete".equals(action)){
		
	}
	return mapping.findForward(forward);
	} 
    private Object[] getParam(HttpServletRequest request){ 
    	return null;
    }

}
